﻿/*******************************************************************************
* Copyright (C) Gasgoo Corporation. All rights reserved.
* 
* Author: Samuel.M.Xu
* Create Date: 2010-09-15
* Description: Gasgoo js framework
*          
* Revision History:
* Date         Author               Description
* 
*********************************************************************************/
// Namespace
window.usingNamespace = function(a) {
    var ro = window;
    if (!(typeof (a) === "string" && a.length != 0)) {
        return ro;
    }
    var co = ro;
    var nsp = a.split(".");
    for (var i = 0; i < nsp.length; i++) {
        var cp = nsp[i];
        if (!ro[cp]) {
            ro[cp] = {};
        };
        co = ro = ro[cp];
    };

    return co;
};

// 浮动层
usingNamespace("Web.UI")["Layer"] = {
    IsBack: false,  // 是否有背景
    IsFooter: true,
    Width: 'auto',
    Title: '',
    Content: '',
    CloseText: 'Close',
    IsConfirm: false,
    Confirm: {
        Text: 'Ok',
        Event: ''
    },
    // 打开浮动层
    Open: function() {
        if ($Layer.IsBack && $("#layerBackground").length == 0) {
            $("body").prepend('<div id="layerBackground" class="setOpacity"></div>');
        }

        if ($("#showBox").length == 0) {
            // 设置window model 是否为confrim，及按钮的文字、事件
            var confirmContent = ""; if ($Layer.IsConfirm) { confirmContent = "<input type='button' onclick='" + $Layer.Confirm.Event + "' value='" + $Layer.Confirm.Text + "' id='btnLayerEnter'/>"; }
            // window width
            var width = $Layer.Width; if (width != "auto") { width = width + "px"; }
            $("body").prepend('<div id="showBox" style="width:' + width + '"><div id="myAlpha" style="background-color:#fff; filter:alpha(opacity=1);opacity:1"></div></div>')
            var header = "<h1>" + $Layer.Title + "</h1><span class='close' onclick='$Layer.Close();'>X</span>";
            var content = "<div style='padding:10px;background:#FFF;'>" + $Layer.Content + "</div>";
            var footer = ""; if ($Layer.IsFooter) { footer = "<div style='background:#EEE;text-align:center;padding:5px 10px;'>" + confirmContent + "<input type='button' onclick='$Layer.Close();' value='" + $Layer.CloseText + "' id='btnLayerClose'/></div>"; }
            var element = document.documentElement;
            $('#showBox').append(header + content + footer);
            // 设置window居中显示
            var top = 0; top = (element.clientHeight - $('#showBox').height()) / 2; if (top < 0) { top = 0; }
            $('#showBox').css({ 'top': top, 'left': (element.clientWidth - $('#showBox').width()) / 2 });
        }
        else {
            $("#showBox").remove();
            $Layer.Open();
        }
    },
    // 关闭浮动层
    Close: function() {
        $("#layerBackground").remove();
        $("#showBox").remove();
    },
    Reset: function() {
        $Layer.IsBack = false;
        $Layer.IsFooter = true;
        $Layer.Width = 'auto';
        $Layer.Title = '';
        $Layer.Content = '';
        $Layer.IsConfirm = false;
        $Layer.Event = '';
    }
};
var $Layer = Web.UI.Layer;